package com.dhkj.circle.impl;

import android.app.Activity;

import com.dhkj.circle.constant.CardConst;
import com.dhkj.circle.db.sqlserver.LSZMWXbaseSQL;
import com.dhkj.circle.thread.ThreadPoolManager;
import com.dhkj.circle.ui.dialog.ZMWXDialog;
import com.dhkj.circle.utils.NFCUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.math.BigDecimal;

import static com.dhkj.circle.ui.AppContext.sLogger;

public class LSZMWXImpl {
    private static LSZMWXImpl lszmwx = new LSZMWXImpl();
    private static LSZMWXshowListener lszmwXshowListener;
    private static LSZMWXWriteListener lszmwxWriteListener;

    public static void setLszmwxWriteListener(LSZMWXWriteListener lszmwxWriteListener) {
        LSZMWXImpl.lszmwxWriteListener = lszmwxWriteListener;
    }

    public static void setLszmwXshowListener(LSZMWXshowListener lszmwXshowListener) {
        LSZMWXImpl.lszmwXshowListener = lszmwXshowListener;
    }

    public static LSZMWXImpl getInstance() {
        return lszmwx;
    }

    private Activity activity;

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void getUserJeByCardNo(String cardNo) {
        ThreadPoolManager.getInstance().executeTask(() -> {
            String str = LSZMWXbaseSQL.getInstance().getUserJE(cardNo) == null ? "0" : LSZMWXbaseSQL.getInstance().getUserJE(cardNo);
//            activity.runOnUiThread(() -> showUI(str));
            if (lszmwXshowListener != null) {
                activity.runOnUiThread(() -> lszmwXshowListener.show("2",str));
            }
        });
    }

    private void showUI(String je) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("学号：" + String.valueOf(NFCUtils.getCardInfo().zyh) + "\n");
        stringBuffer.append("姓名：" + NFCUtils.getCardInfo().name + "\n\n");
        stringBuffer.append("饭卡\n");
        stringBuffer.append("卡上金额：" + BigDecimal.valueOf(NFCUtils.getCardInfo().je).divide(BigDecimal.valueOf(100)) + "\t元\n");
        stringBuffer.append("待充值金额：" + je + "\t元\n\n");
        new QMUIDialog.MessageDialogBuilder(activity)
                .setTitle("查询")
                .setMessage(stringBuffer.toString())
                .addAction("返回", (QMUIDialog dialog, int index) ->dialog.dismiss())
                .show();
    }

    public void getUserJeByCardNo2(String cardNo) {
        ThreadPoolManager.getInstance().executeTask(() -> {
            String str = LSZMWXbaseSQL.getInstance().getUserJE(cardNo) == null ? "0" : LSZMWXbaseSQL.getInstance().getUserJE(cardNo);
            if (str == null || equalTod(BigDecimal.valueOf(Double.parseDouble(str)), BigDecimal.valueOf(0.0))) {
                if (lszmwXshowListener != null) {
                    activity.runOnUiThread(() -> lszmwXshowListener.show("1",null));
                }
            } else {
                CardConst.setJe(str);
                sLogger.d("发现余额，正在操作数据库，写库操作");
                int count = LSZMWXbaseSQL.getInstance().upJeState(String.valueOf(CardConst.getCardInfo().zyh).trim());
                if (count > 0) {
                    sLogger.d("成功修改数据库，返回信息进行写卡操作");
                    if (lszmwXshowListener != null) {
                        activity.runOnUiThread(() -> lszmwxWriteListener.write());
                    }
                }
            }
        });
    }

    public interface LSZMWXshowListener {
        void show(String type,String je);
    }

    public interface LSZMWXWriteListener {
        void write();
    }

    public static boolean equalTod(BigDecimal b1, BigDecimal b2) {
        if (b1 == null || b2 == null) {
            return false;
        }
        return 0 == b1.compareTo(b2);
    }
}
